//
//  CustomDropdownMenuView.m
//  NovartisPPH
//
//  Created by Sidney on 13-10-10.
//  Copyright (c) 2013年 iSoftstone infomation Technology (Group) Co.,Ltd. All rights reserved.
//

#import "CustomDropdownMenuView.h"

@interface CellView : UIView

@property(nonatomic , strong) UILabel * titleLabel;

@end


@implementation CellView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGRect tempFrame = self.frame;
        tempFrame.origin.x = 5;
        tempFrame.size.width = CGRectGetWidth(frame) - 10;
        self.titleLabel = [[UILabel alloc] initWithFrame:tempFrame];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.numberOfLines = 3;
        
        [self addSubview:self.titleLabel];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


@end

@interface CustomDropdownMenuView()
<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) UITableView * tableView;
@property (nonatomic , strong) NSMutableArray * allCellViewArr;
@property (nonatomic , strong) NSMutableArray * allCellDataArr;
@property(nonatomic , assign) int cellHeight;

@property (nonatomic , strong) UIView * bgView;

@end

@implementation CustomDropdownMenuView
@synthesize allCellViewArr,bgView,allCellDataArr,cellHeight;


- (id)initWithContentSize:(CGSize)size type:(DROP_DOWN_MENU_VIEW_TYPE)type
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        _mType = type;
        allCellViewArr = [[NSMutableArray alloc] init];
        allCellDataArr = [[NSMutableArray alloc] init];
        
        switch (_mType) {
            case DROP_DOWN_MENU_VIEW_TYPE_DEFAULT:
            {
                cellHeight = 40;
                bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
                bgView.backgroundColor = [UIColor clearColor];
                [bgView.layer setMasksToBounds:YES];
                
                UIImage * bgImg = [UIImage createImageWithBgColor:MAIN_GRAY_COLOR size:size cornerRadius:0];
                bgImg = [bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(3, 10, 20, 20)];
                UIImageView * bg = [[UIImageView alloc] initWithFrame:bgView.bounds];
                bg.image = bgImg;
                [bgView addSubview:bg];
                
                _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bgView.frame), CGRectGetHeight(bgView.frame) - 6)];
                _tableView.backgroundColor = [UIColor clearColor];
                _tableView.separatorColor = MAIN_BLACK_COLOR;
//                [_tableView.layer setCornerRadius:8];
                _tableView.delegate = self;
                _tableView.dataSource = self;
                [bgView addSubview:_tableView];
                _isOpened = NO;
                
            }
                break;
                
            default:
                break;
        }
    }
    return self;
}


- (void)openSubMenuWithPostion:(CGPoint)postion
{
    float width = CGRectGetWidth(bgView.frame);
    switch (_mType) {
        case DROP_DOWN_MENU_VIEW_TYPE_DEFAULT:
        {
            float height = CGRectGetHeight(_tableView.frame) + 6;
            bgView.frame = CGRectMake(postion.x, postion.y, width, 0);
            [UIView animateWithDuration:0.3f animations:^{
                bgView.frame = CGRectMake(postion.x, postion.y, width, height);
                [self addSubview:bgView];
            }];
            
        }
            break;
        default:
            break;
    }
    _isOpened = YES;
}


- (void)setMenuData:(NSArray *)data
{
    for (int i = 0; i < [data count]; i++) {
        NSDictionary * c_dic = [data objectAtIndex:i];
        NSString * c_name = [c_dic objectForKey:@"name"];
        CellView * cell = [[CellView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bgView.frame), cellHeight)];
        cell.titleLabel.text = [NSString stringWithFormat:@" %@",c_name];
        [allCellViewArr addObject:cell];
        [allCellDataArr addObject:c_dic];
    }
    [self.tableView reloadData];
}


- (void)closed
{
    if (!_isOpened) return;
    
    [UIView animateWithDuration:0.3f animations:^{
        switch (_mType) {
            case DROP_DOWN_MENU_VIEW_TYPE_DEFAULT:
            {
                CGRect tempFrame = bgView.frame;
                tempFrame.size.height = 0;
                bgView.frame = tempFrame;
            }
                break;
            default:
                break;
        }
        
        
        
    } completion:^(BOOL finished) {
        [bgView removeFromSuperview];
        [self removeFromSuperview];
        _isOpened = NO;
    }];
    
    if ([_delegate respondsToSelector:@selector(dropdownMenuColosed:)]) {
        [_delegate dropdownMenuColosed:self];
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self closed];
}


#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [allCellViewArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        UIImageView * selectedBg = [[UIImageView alloc] initWithFrame:cell.bounds];
        
        [cell setSelectedBackgroundView:selectedBg];
    }
    
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSInteger row = indexPath.row;
    [cell.contentView addSubview:[allCellViewArr objectAtIndex:row]];
    
    return cell;
}

//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    switch (_mType) {
//        case DROP_DOWN_MENU_VIEW_TYPE_DEFAULT:
//        {
//            NSDictionary * tempDic = [_allSectionViewArr objectAtIndex:section];
//            return [tempDic objectForKey:[NSString stringWithFormat:@"%d-cell",section]];
//        }
//            break;
//        default:
//            return nil;
//            break;
//    }
//}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    switch (tableType) {
//        case TABLE_VIEW_TYPE_RESOURCE_SUB_MENU:
//            return cellHeight;
//            break;
//        default:
//            return 0;
//            break;
//    }
//}


//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    switch (tableType) {
//        case TABLE_VIEW_TYPE_RESOURCE_SUB_MENU:
//        {
//            NSDictionary * tempDic = [allSectionViewArr objectAtIndex:section];
//            return [tempDic objectForKey:[NSString stringWithFormat:@"%d-cell",section]];
//        }
//            break;
//        default:
//            return nil;
//            break;
//    }
//}

#pragma mark TableviewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self closed];
    
    if ([_delegate respondsToSelector:@selector(cellViewSelected:data:)]) {
        [_delegate cellViewSelected:self data:[allCellDataArr objectAtIndex:row]];
    }
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
